package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;

import static com.shanxia.codec.CodeConstant.HUNDRED;
import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.*;

/**
 * S -> C
 * 后台服务器设置 24 时电费计价策略信息，若平台存在跨时段服务费，请使用CMD1105
 */
@Data
public class Cmd1103 extends ProtocolDataBase {

    public static final int MIN_SIZE = 96;

    private int startHour1;                                 // 开始小时
    private int startMinute1;                               // 开始分钟   
    private int endHour1;                                   // 结束小时                
    private int endMinute1;                                 // 结束分钟    
    private double rate1;                                   // 费率    
    private int startHour2;
    private int startMinute2;
    private int endHour2;
    private int endMinute2;
    private double rate2;
    private int startHour3;
    private int startMinute3;
    private int endHour3;
    private int endMinute3;
    private double rate3;
    private int startHour4;
    private int startMinute4;
    private int endHour4;
    private int endMinute4;
    private double rate4;
    private int startHour5;
    private int startMinute5;
    private int endHour5;
    private int endMinute5;
    private double rate5;
    private int startHour6;
    private int startMinute6;
    private int endHour6;
    private int endMinute6;
    private double rate6;
    private int startHour7;
    private int startMinute7;
    private int endHour7;
    private int endMinute7;
    private double rate7;
    private int startHour8;
    private int startMinute8;
    private int endHour8;
    private int endMinute8;
    private double rate8;
    private int startHour9;
    private int startMinute9;
    private int endHour9;
    private int endMinute9;
    private double rate9;
    private int startHour10;
    private int startMinute10;
    private int endHour10;
    private int endMinute10;
    private double rate10;
    private int startHour11;
    private int startMinute11;
    private int endHour11;
    private int endMinute11;
    private double rate11;
    private int startHour12;
    private int startMinute12;
    private int endHour12;
    private int endMinute12;
    private double rate12;

    public Cmd1103(){
        super.setCmd(1103);
    }

    public static Cmd1103 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1103 cmd = new Cmd1103();
        cmd.copyDataFrom(protocolDataBase);
        //数据域长度
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        // 1字节开始小时
        cmd.startHour1 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute1 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour1 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute1 = getUint8(dataField[offset++]);
        // 4字节费率1
        cmd.rate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour2 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute2 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour2 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute2 = getUint8(dataField[offset++]);
        // 4字节费率2
        cmd.rate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour3 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute3 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour3 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute3 = getUint8(dataField[offset++]);
        // 4字节费率3
        cmd.rate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour4 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute4 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour4 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute4 = getUint8(dataField[offset++]);
        // 4字节费率4
        cmd.rate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour5 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute5 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour5 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute5 = getUint8(dataField[offset++]);
        // 4字节费率5
        cmd.rate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour6 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute6 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour6 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute6 = getUint8(dataField[offset++]);
        // 4字节费率6
        cmd.rate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour7 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute7 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour7 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute7 = getUint8(dataField[offset++]);
        // 4字节费率7
        cmd.rate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour8 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute8 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour8 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute8 = getUint8(dataField[offset++]);
        // 4字节费率8
        cmd.rate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour9 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute9 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour9 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute9 = getUint8(dataField[offset++]);
        // 4字节费率9
        cmd.rate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour10 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute10 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour10 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute10 = getUint8(dataField[offset++]);
        // 4字节费率10
        cmd.rate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour11 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute11 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour11 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute11 = getUint8(dataField[offset++]);
        // 4字节费率11
        cmd.rate11 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour12 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute12 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour12 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute12 = getUint8(dataField[offset++]);
        // 4字节费率12
        cmd.rate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        int offset = 0;
        // 1字节开始小时
        data[offset++] = (byte) startHour1;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute1;
        // 1字节结束小时
        data[offset++] = (byte) endHour1;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute1;
        // 4字节费率1
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate1)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour2;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute2;
        // 1字节结束小时
        data[offset++] = (byte) endHour2;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute2;
        // 4字节费率2
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate2)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour3;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute3;
        // 1字节结束小时
        data[offset++] = (byte) endHour3;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute3;
        // 4字节费率3
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate3)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour4;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute4;
        // 1字节结束小时
        data[offset++] = (byte) endHour4;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute4;
        // 4字节费率4
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate4)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour5;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute5;
        // 1字节结束小时
        data[offset++] = (byte) endHour5;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute5;
        // 4字节费率5
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate5)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour6;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute6;
        // 1字节结束小时
        data[offset++] = (byte) endHour6;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute6;
        // 4字节费率6
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate6)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour7;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute7;
        // 1字节结束小时
        data[offset++] = (byte) endHour7;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute7;
        // 4字节费率7
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate7)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour8;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute8;
        // 1字节结束小时
        data[offset++] = (byte) endHour8;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute8;
        // 4字节费率8
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate8)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour9;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute9;
        // 1字节结束小时
        data[offset++] = (byte) endHour9;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute9;
        // 4字节费率9
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate9)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour10;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute10;
        // 1字节结束小时
        data[offset++] = (byte) endHour10;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute10;
        // 4字节费率10
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate10)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour11;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute11;
        // 1字节结束小时
        data[offset++] = (byte) endHour11;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute11;
        // 4字节费率11
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate11)).multiply(HUNDRED).intValue());
        // 1字节开始小时
        data[offset++] = (byte) startHour12;
        // 1字节开始分钟
        data[offset++] = (byte) startMinute12;
        // 1字节结束小时
        data[offset++] = (byte) endHour12;
        // 1字节结束分钟
        data[offset++] = (byte) endMinute12;
        // 4字节费率12
        intToBytesLittle(data, offset, new BigDecimal(String.valueOf(rate12)).multiply(HUNDRED).intValue());
        return data;
    }
}
